package cc.catface.api;

import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


@Component
@EnableOpenApi
@Data//读配置
@ConfigurationProperties("swagger")
public class SwaggerConfiguration {
    private Boolean enable;//开启swagger
    private String applicationName;//项目名
    private String applicationVersion;//项目版本
    private String applicationDescription;//项目描述

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)//OPEN API3.0
                .pathMapping("/")
                .enable(enable)
                .apiInfo(apiInfo())//文档信息
                .groupName("默认")
                .select()//选择哪些接口作为swagger的doc发布
                //RequestHandlerSelectors.any()所有
                //RequestHandlerSelectors.basePackage("cc.catface.*")指定包
                //RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)含注解ApiOperation
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//控制暴漏的接口
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(applicationName)
                .description(applicationDescription)
                .contact(new Contact("catface", "catface.cc", "13012892925@163.com"))
                .version(applicationVersion)
                .build();
    }
}
